package com.lyhengtongwl.zqsnews.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.ui.fragment.HomeFragment;
import com.lyhengtongwl.zqsnews.utils.SimpleCountDownTimer;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by Steven on 2019/1/9.
 */
public class HomeBottomRecycleAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {
    private JSONArray goodsList;
    private Map<String, SimpleCountDownTimer> map;

    public HomeBottomRecycleAdapter(JSONArray goodsList) {
        super(R.layout.home_bottom_item, goodsList);
        this.goodsList = goodsList;
        map = new HashMap<>();
    }


    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        if (((helper.getPosition() + 1) % 2) == 0) {
            //表示偶数
            setVisible(helper, item, true);
        } else {
            //表示奇数
            setVisible(helper, item, false);
        }
    }

    //清楚 单个时间控制器
    public void cancelOneTimes(String goodId) {
        if (map == null) {
            return;
        }
        Set<Map.Entry<String, SimpleCountDownTimer>> set = map.entrySet();

        Iterator<Map.Entry<String, SimpleCountDownTimer>> iterator = set.iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, SimpleCountDownTimer> entry = iterator.next();
            String name = entry.getKey();
            SimpleCountDownTimer value = entry.getValue();
            if (name.contains(goodId)) {
                value.cancel();
                //特别注意：不能使用map.remove(name)  否则会报同样的错误
                iterator.remove();
            }
        }
    }

    /**
     * 清空所有时间控制器资源
     */
    public void cancelAllTimers() {
        if (map == null) {
            return;
        }
        Set<Map.Entry<String, SimpleCountDownTimer>> set = map.entrySet();

        Iterator<Map.Entry<String, SimpleCountDownTimer>> iterator = set.iterator();

        while (iterator.hasNext()) {
            Map.Entry<String, SimpleCountDownTimer> entry = iterator.next();
            SimpleCountDownTimer value = entry.getValue();
            value.cancel();
            //特别注意：不能使用map.remove(name)  否则会报同样的错误
            iterator.remove();
        }
    }

    @SuppressLint("SetTextI18n")
    private void setVisible(BaseViewHolder helper, Object item, boolean visible) {
        if (visible) {
            helper.getView(R.id.left_good).setBackgroundResource(R.drawable.home_right_shape);
        } else {
            helper.getView(R.id.left_good).setBackgroundResource(R.drawable.home_left_shape);
        }
        JSONObject jsonObject = JSON.parseObject(goodsList.getString(helper.getPosition()));
        String salesType = jsonObject.getString("salesType");
        String originalPrice = jsonObject.getString("originalPrice");
        String[] str = (originalPrice.trim()).split("\\.");
        if (str.length == 2) {
            if (("0").equals(str[0]) && ("00").equals(str[1])) {
                helper.getView(R.id.left_goodM2).setVisibility(View.GONE);
                helper.getView(R.id.tv_fuhao).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.left_goodM2).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_fuhao).setVisibility(View.VISIBLE);
            }
        } else if (str.length == 0) {
            if (("0").equals(originalPrice)) {
                helper.getView(R.id.left_goodM2).setVisibility(View.GONE);
                helper.getView(R.id.tv_fuhao).setVisibility(View.GONE);
            }
        }
        switch (salesType) {
            case "0"://普通商品left_goodName
            case "2"://拼团
                (helper.getView(R.id.rl_bid)).setVisibility(View.GONE);
                ((TextView) helper.getView(R.id.left_goodM1)).setText(jsonObject.getString("discountPrice"));
                ((TextView) helper.getView(R.id.left_goodM2)).setText(originalPrice);
                break;
            case "1"://竞拍
                (helper.getView(R.id.rl_bid)).setVisibility(View.VISIBLE);
                setBid(jsonObject, helper);
                TextView textView = helper.getView(R.id.left_goodM1);
                String money = jsonObject.getString("biddingPrice");
                if (!textView.getText().toString().equals(money)) {
                    textView.setText(money);
                    if (!HomeFragment.isLoadData) {
                        ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(App.getContext(), R.anim.zoom_scale);
                        helper.getView(R.id.ll_nowMoney).startAnimation(scaleAnimation);
                    }
                }
                ((TextView) helper.getView(R.id.left_goodM2)).setText(jsonObject.getString("originalPrice"));
                break;
        }
        Picasso.with(App.getContext()).load(jsonObject.getString("previewImage")).placeholder(R.drawable.home_product).error(R.drawable.home_product).into(((ImageView) helper.getView(R.id.left_goodImg)));
        ((TextView) helper.getView(R.id.left_goodName)).setText(jsonObject.getString("goodsTitle"));
        ((TextView) helper.getView(R.id.left_goodM2)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }

    //设置 竞拍显示
    @SuppressLint("ResourceAsColor")
    private void setBid(JSONObject object, final BaseViewHolder helper) {
        switch (object.getString("goodsStatus")) {
            case "10"://预售中   需要取发行倒计时    publishCountdown 本地进行倒计时
                ((TextView) helper.getView(R.id.tv_status)).setText("预");
                helper.getView(R.id.tv_status).setBackgroundResource(R.drawable.bid_pre);
                helper.getView(R.id.tv_bidding).setBackgroundResource(R.color.colorFFDFCA);
                ((TextView)helper.getView(R.id.tv_bidding)).setTextColor(Color.parseColor("#FF934B"));
                if (object.containsKey("publishCountdown")) {
                    if (!"0".equals(object.getString("publishCountdown"))) {
                        SimpleCountDownTimer simpleCountDownTimer = new SimpleCountDownTimer(Long.parseLong(object.getString("publishCountdown")), ((TextView) helper.getView(R.id.tv_bidding)));
                        simpleCountDownTimer.start();
                        map.put(object.getString("goodsId"), simpleCountDownTimer);
                    }
                }
                break;
            case "20"://竞拍中
                ((TextView) helper.getView(R.id.tv_status)).setText("拍");
                helper.getView(R.id.tv_status).setBackgroundResource(R.drawable.bid_pat);
                helper.getView(R.id.tv_bidding).setBackgroundResource(R.color.colorFFDBD4);
                ((TextView)helper.getView(R.id.tv_bidding)).setTextColor(Color.parseColor("#FF392F"));
                SimpleCountDownTimer simpleCountDownTimer = new SimpleCountDownTimer(Long.parseLong(object.getString("biddingCountdown")), ((TextView) helper.getView(R.id.tv_bidding)));
                simpleCountDownTimer.start();
                map.put(object.getString("goodsId"), simpleCountDownTimer);
                break;
            case "40"://已下架
            case "31"://已成交
            case "30"://已售罄
                ((TextView) helper.getView(R.id.tv_status)).setText("罄");
                helper.getView(R.id.tv_status).setBackgroundResource(R.drawable.bid_stop);
                helper.getView(R.id.tv_bidding).setBackgroundResource(R.color.colorEEE);
                break;
        }
    }
}
